<template>
  <div>
    <el-card shadow="never">
      <el-skeleton :loading="loading" animated>
        <el-row :gutter="16" justify="space-between">
          <el-col :xl="12" :lg="12" :md="12" :sm="24" :xs="24">
            <div class="flex items-center">
              <el-avatar :src="avatar" :size="70" class="mr-16px">
                <img src="@/assets/imgs/avatar.gif" alt="" />
              </el-avatar>
              <div>
                <div class="text-20px">
                  {{ t('workplace.welcome') }} {{ username }} {{ t('workplace.happyDay') }}
                </div>

              </div>
            </div>
          </el-col>
<!--          <el-col :xl="12" :lg="12" :md="12" :sm="24" :xs="24">-->
<!--            <div class="h-70px flex items-center justify-end lt-sm:mt-10px">-->
<!--              <div class="px-8px text-right">-->
<!--                <div class="mb-16px text-14px text-gray-400">{{ t('workplace.project') }}</div>-->
<!--                <CountTo-->
<!--                  class="text-20px"-->
<!--                  :start-val="0"-->
<!--                  :end-val="totalSate.project"-->
<!--                  :duration="2600"-->
<!--                />-->
<!--              </div>-->
<!--              <el-divider direction="vertical" />-->
<!--              <div class="px-8px text-right">-->
<!--                <div class="mb-16px text-14px text-gray-400">{{ t('workplace.toDo') }}</div>-->
<!--                <CountTo-->
<!--                  class="text-20px"-->
<!--                  :start-val="0"-->
<!--                  :end-val="totalSate.todo"-->
<!--                  :duration="2600"-->
<!--                />-->
<!--              </div>-->
<!--              <el-divider direction="vertical" border-style="dashed" />-->
<!--              <div class="px-8px text-right">-->
<!--                <div class="mb-16px text-14px text-gray-400">{{ t('workplace.access') }}</div>-->
<!--                <CountTo-->
<!--                  class="text-20px"-->
<!--                  :start-val="0"-->
<!--                  :end-val="totalSate.access"-->
<!--                  :duration="2600"-->
<!--                />-->
<!--              </div>-->
<!--            </div>-->
<!--          </el-col>-->
        </el-row>
      </el-skeleton>
    </el-card>
  </div>

  <el-row class="mt-8px" :gutter="8" justify="space-between">
    <el-col :xl="24" :lg="24" :md="24" :sm="24" :xs="24" class="mb-8px">
      <el-card shadow="never">

        <el-skeleton :loading="loading" animated>
          <el-row>
            <el-col
              v-for="(item, index) in projects"
              :key="`card-${index}`"
              :xl="8"
              :lg="8"
              :md="8"
              :sm="24"
              :xs="24"
            >
              <el-card
                shadow="hover"
                class="mr-5px mt-5px cursor-pointer"

              >
                <div class="flex items-center">
                  <Icon
                    :icon="item.icon"
                    :size="25"
                    class="mr-8px"
                    :style="{ color: item.color }"
                  />
                  <span class="text-24px">{{ item.name }}</span>
                </div>
<!--                <div class="mt-12px text-12px text-gray-400">{{ t(item.message) }}</div>-->
                <div class="mt-12px flex justify-between text-36px text-gray-400">
                  <span>{{ item.personal }}</span>
<!--                  <span>{{ formatTime(item.time, 'yyyy-MM-dd') }}</span>-->
                </div>
              </el-card>
            </el-col>
          </el-row>
        </el-skeleton>
      </el-card>

      <el-card shadow="never" class="mt-8px">
        <el-skeleton :loading="loading" animated>
          <el-row :gutter="20" justify="space-between">
            <el-col :xl="12" :lg="10" :md="24" :sm="24" :xs="24">
              <el-card shadow="hover" class="mb-8px">
                <el-skeleton :loading="loading" animated>
                  <Echart :options="lineOptionsData" :height="280" />
                </el-skeleton>
              </el-card>
            </el-col>
            <el-col :xl="12" :lg="14" :md="24" :sm="24" :xs="24">
              <el-card shadow="hover" class="mb-8px">
                <el-skeleton :loading="loading" animated>
                  <Echart :options="barOptionsData" :height="280" />
                </el-skeleton>
              </el-card>
            </el-col>
          </el-row>
        </el-skeleton>
      </el-card>
    </el-col>
<!--    <el-col :xl="8" :lg="8" :md="24" :sm="24" :xs="24" class="mb-8px">-->
<!--      <el-card shadow="never">-->
<!--        <template #header>-->
<!--          <div class="h-3 flex justify-between">-->
<!--            <span>{{ t('workplace.shortcutOperation') }}</span>-->
<!--          </div>-->
<!--        </template>-->
<!--        <el-skeleton :loading="loading" animated>-->
<!--          <el-row>-->
<!--            <el-col v-for="item in shortcut" :key="`team-${item.name}`" :span="8" class="mb-8px">-->
<!--              <div class="flex items-center">-->
<!--                <Icon :icon="item.icon" class="mr-8px" :style="{ color: item.color }" />-->
<!--                <el-link type="default" :underline="false" @click="handleShortcutClick(item.url)">-->
<!--                  {{ item.name }}-->
<!--                </el-link>-->
<!--              </div>-->
<!--            </el-col>-->
<!--          </el-row>-->
<!--        </el-skeleton>-->
<!--      </el-card>-->
<!--      <el-card shadow="never" class="mt-8px">-->
<!--        <template #header>-->
<!--          <div class="h-3 flex justify-between">-->
<!--            <span>{{ t('workplace.notice') }}</span>-->
<!--            <el-link type="primary" :underline="false">{{ t('action.more') }}</el-link>-->
<!--          </div>-->
<!--        </template>-->
<!--        <el-skeleton :loading="loading" animated>-->
<!--          <div v-for="(item, index) in notice" :key="`dynamics-${index}`">-->
<!--            <div class="flex items-center">-->
<!--              <el-avatar :src="avatar" :size="35" class="mr-16px">-->
<!--                <img src="@/assets/imgs/avatar.gif" alt="" />-->
<!--              </el-avatar>-->
<!--              <div>-->
<!--                <div class="text-14px">-->
<!--                  <Highlight :keys="item.keys.map((v) => t(v))">-->
<!--                    {{ item.type }} : {{ item.title }}-->
<!--                  </Highlight>-->
<!--                </div>-->
<!--                <div class="mt-16px text-12px text-gray-400">-->
<!--                  {{ formatTime(item.date, 'yyyy-MM-dd') }}-->
<!--                </div>-->
<!--              </div>-->
<!--            </div>-->
<!--            <el-divider />-->
<!--          </div>-->
<!--        </el-skeleton>-->
<!--      </el-card>-->
<!--    </el-col>-->
  </el-row>
</template>
<script lang="ts" setup>
import { set } from 'lodash-es'
import { EChartsOption } from 'echarts'
import { formatTime } from '@/utils'

import { useUserStore } from '@/store/modules/user'
// import { useWatermark } from '@/hooks/web/useWatermark'
import type { WorkplaceTotal, Project, Notice, Shortcut } from './types'
import {pieOptions, barOptions, lineOptions} from './echarts-data'
import { useRouter } from 'vue-router'
import {PurOrderApi} from "@/api/ck/purorder";

defineOptions({ name: 'Index' })

const { t } = useI18n()
const router = useRouter()
const userStore = useUserStore()
// const { setWatermark } = useWatermark()
const loading = ref(true)
const avatar = userStore.getUser.avatar
const username = userStore.getUser.nickname
const pieOptionsData = reactive<EChartsOption>(pieOptions) as EChartsOption

let initData  = ref() // 搜索的表单


// 获取项目数
let projects = reactive<Project[]>([])
const getProject = async () => {

  loading.value = true

  try {
    initData = await PurOrderApi.getDashboardPage("")
    console.log("======")
    console.log(initData)
  } finally {
    loading.value = false
  }

  const data = [
    {
      name: '当日新增订单数',
      icon: 'simple-icons:springboot',
      message: 'github.com/YunaiV/ruoyi-vue-pro',
      personal: initData.orderCountByDate,
      time: new Date(),
      color: '#6DB33F'
    },
    {
      name: '当日新增送货计划',
      icon: 'ep:element-plus',
      message: 'github.com/yudaocode/yudao-ui-admin-vue3',
      personal: initData.planCountByDate,
      time: new Date(),
      color: '#409EFF'
    },
    {
      name: '当日待送货',
      icon: 'icon-park-outline:mall-bag',
      message: 'github.com/yudaocode/yudao-ui-mall-uniapp',
      personal: initData.orderCount4UnSend,
      time: new Date(),
      color: '#ff4d4f'
    },
    {
      name: '当日已送货',
      icon: 'material-symbols:cloud-outline',
      message: 'github.com/YunaiV/yudao-cloud',
      personal: initData.orderCount4Send,
      time: new Date(),
      color: '#1890ff'
    },
    {
      name: '当日新增订单金额',
      icon: 'devicon:antdesign',
      message: 'github.com/yudaocode/yudao-ui-admin-vben',
      personal: initData.orderCountByDateTotalAmount,
      time: new Date(),
      color: '#e18525'
    },
    {
      name: '当日新增送货单',
      icon: 'ant-design:mobile',
      message: 'github.com/yudaocode/yudao-ui-admin-uniapp',
      personal: initData.deliveryCountByDate,
      time: new Date(),
      color: '#2979ff'
    }
  ]
  projects = Object.assign(projects, data)

  getMonthlySales()
  getWeeklyOrders()
}



const barOptionsData = reactive<EChartsOption>(barOptions) as EChartsOption


const lineOptionsData = reactive<EChartsOption>(lineOptions) as EChartsOption

// 每月销售总额
const getMonthlySales = async () => {
  // const data = [
  //   {  actual: 120, name: 'analysis.january' },
  //   {  actual: 82, name: 'analysis.february' },
  //   {  actual: 91, name: 'analysis.march' },
  //   {  actual: 154, name: 'analysis.april' },
  //   {  actual: 162, name: 'analysis.may' },
  //   {  actual: 140, name: 'analysis.june' },
  //   {  actual: 145, name: 'analysis.july' },
  //   {  actual: 250, name: 'analysis.august' },
  //   {  actual: 134, name: 'analysis.september' },
  //   {  actual: 56, name: 'analysis.october' },
  //   {  actual: 99, name: 'analysis.november' },
  //   {  actual: 123, name: 'analysis.december' }
  // ]


  const data =initData.orderCountByMonthList
  set(
    lineOptionsData,
    'xAxis.data',
    data.map((v) => t(v.name))
  )
  set(lineOptionsData, 'series', [

    {
      name: t('analysis.actual'),
      smooth: true,
      type: 'line',
      itemStyle: {},
      data: data.map((v) => v.actual),
      animationDuration: 2800,
      animationEasing: 'quadraticOut'
    }
  ])
}
// 日订单数
const getWeeklyOrders = async () => {

  const data =initData.orderCountByDayList
  set(
    barOptionsData,
    'xAxis.data',
    data.map((v) => t(v.name))
  )
  set(barOptionsData, 'series', [
    {
      name: '订单',
      data: data.map((v) => v.actual),
      type: 'bar'
    }
  ])
}

const getAllApi = async () => {
  await Promise.all([
    getProject(),
    getWeeklyOrders()

  ])
  loading.value = false
}



getAllApi()
</script>
